package com.tao.system.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.tao.common.creo.domain.Result;
import com.tao.common.creo.enums.ResultCode;
import com.tao.common.security.exception.ServiceException;
import com.tao.system.domain.question.Question;
import com.tao.system.domain.question.dto.QuestionAddDTO;
import com.tao.system.domain.question.dto.QuestionEditDTO;
import com.tao.system.domain.question.dto.QuestionQueryDTO;
import com.tao.system.domain.question.vo.QuestionDetailVO;
import com.tao.system.domain.question.vo.QuestionVO;
import com.tao.system.mapper.question.QuestionMapper;
import com.tao.system.service.question.IQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    QuestionMapper questionMapper;


    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {
        // 判断是否传递竞赛列表下的题目列表
        String excludeIdStr = questionQueryDTO.getExcludeIdStr();
        if(StrUtil.isNotBlank(excludeIdStr)) {
            // 拆分字符串
            String[] excludeArr = excludeIdStr.split(";");
            Set<Long> excludeIdSet = Arrays.stream(excludeArr)
                    .map(Long::valueOf).
                    collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(excludeIdSet);
        }
        PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
        List<QuestionVO> questionVO = questionMapper.selectQuestionList(questionQueryDTO);

        return questionVO;
    }

    @Override
    public int add(QuestionAddDTO questionAddDTO) {
        // 校验题目是否存在
        List<Question> questions = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle, questionAddDTO.getTitle()));
        // 校验题目标题是否存在
        if(CollectionUtil.isNotEmpty(questions)) {
            throw new ServiceException(ResultCode.FAILED_QUESTION_EXISTS);
        }
        // cp 请求对象为实体对象
        Question question = new Question();
        BeanUtil.copyProperties(questionAddDTO, question);
        // 插入数据库
        return questionMapper.insert(question);
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = getQuestionById(questionId);
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtil.copyProperties(question, questionDetailVO);
        return questionDetailVO;
    }

    /**
     * 根据题目id获取题目详情
     */
    private Question getQuestionById(Long questionId) {
        // 从数据库查询
        Question question = questionMapper.selectById(questionId);
        // 校验题目是否存在
        if(question == null) {
            throw new ServiceException(ResultCode.QUESTION_NOT_EXISTS);
        }
        return question;
    }

    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        // 从数据库中拿旧的题目
        Question oldQuestion = getQuestionById(questionEditDTO.getQuestionId());
        // 将新的数据写入到数据库
//        oldQuestion.setQuestionId(questionEditDTO.getQuestionId());
//        oldQuestion.setTitle(questionEditDTO.getTitle());
//        oldQuestion.setDifficulty(questionEditDTO.getDifficulty());
//        oldQuestion.setTimeLimit(questionEditDTO.getTimeLimit());
//        oldQuestion.setSpaceLimit(questionEditDTO.getSpaceLimit());
//        oldQuestion.setContent(questionEditDTO.getContent());
//        oldQuestion.setQuestionCase(questionEditDTO.getQuestionCase());
//        oldQuestion.setDefaultCase(questionEditDTO.getDefaultCase());
//        oldQuestion.setMainFun(questionEditDTO.getMainFun());
        BeanUtil.copyProperties(questionEditDTO, oldQuestion);

        // 修改数据库中的数据
        return questionMapper.updateById(oldQuestion);
    }


    @Override
    public int delete(Long questionId) {
        Question questionById = getQuestionById(questionId);
        return questionMapper.delete(new LambdaQueryWrapper<Question>().eq(Question::getQuestionId, questionId));
    }
}
